# Copyright (c) 2008 Alvin Wang http://cloudwizard.com/
# License New BSD
# See README for additional information

"""Base Object for Cloud Wizard cloud interfaces"""
import ConfigParser
import os
import platform
from optparse import OptionParser
from config import Config
import urllib
#import cubictemp

class Cloud( object):
    """Generally cloud covers a vendor and authentication.

    Eg] Amazon Web Services, GoGrid, Mosso, Flexiscale, etc.

    API
    parsing
    wait_

    Naming convention
    1) Verb_noun
    2) cloud specific functions should be preceeded by the cloud_abbrev
    """

    cloud_name = "Unknown"
    cloud_abbrev = "Unknown"
    cloud_vartypes = {}
    cloud_services = {}
    cloud_obj = None
    cloud_auth = None
    config = {}
    namespace = {}
    log_file = None
    log_level = 50
    auth = {} 

    def __init__(self, username='default', directory=None, autoload=True):
        """
        calling parameters
        init= true - only create config file
                only possible

        command line parameters
        -h, --help = help
        -l, --logfile filename
        -d, --logdir directory name
        -e, --error level
        -c, --config filename
        -t, no ini files, type them in
        -o, --overwrite overwrite the current ini files

        Ini loading order
        if ini files exist use them for authentication
        if ini file exists for this
        """
        if autoload:
            self.cloud_auth = Config( cloud_abbrev=self.cloud_abbrev, \
                type='user', config_name=username, action='load') 

    def script_auth(self):
        """
        Standard function for setting the authentication files
        ** should be over ridden
        """
        pass

    def get_service(self, service=None, namespace=None, **args):
        """return a service object"""
        print "vm_launch is not supported in "+self.cloud_name

    def list_services(self, **args):
        """Return a list of available services"""
        return self.cloud_services

    def print_services(self, **args):
        """
        """
        for service in self.cloud_services:
            print service

    def load_template(self, service=None, namespace=None, **args):
        """
        print cubictemp.File(
        "test.tpl",
        foo=3,
        mydict={"key":"value", "key2":"vale"},
        t1=[
        {"f1":"val1a","f2":"val2a","f3":"val3a"},
        {"f1":"val1b","f2":"val2b","f3":"val3b"},
        {"f1":"val1c","f2":"val2c","f3":"val3c"}]
        )
        """
        print "vm_launch is not supported in "+self.cloud_name
        
    def help(self):
        help(self)
        
    def get_my_ip(self):
        """Get my current IP - mainly useful behind NAT Firewalls"""
        u = urllib.urlopen( 'http://whatismyip.org/')
        return u.read()
